package com.wngbms.projectreactor;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
public class UserService {

    public Flux<String> getFavorites(String userId) {
        log.info("userId:{}", userId);

        List<String> favoriteIds = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            sleep(200);
            log.info("get favorite: {}", i);
            favoriteIds.add(UUID.randomUUID().toString());
        }

        return Flux.fromIterable(favoriteIds);
    }

    public Flux<String> getCachedFavorites(String userId) {
        log.info("getCachedFavorites userId:{}", userId);

        List<String> favoriteIds = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            sleep(200);
            log.info("getCachedFavorites: {}", i);
            favoriteIds.add(UUID.randomUUID().toString());
        }

        return Flux.fromIterable(favoriteIds);
    }

    public static void sleep(long l) {
        try {
            TimeUnit.MILLISECONDS.sleep(l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
